﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WCFMINERP.InnerFactoryContracts.DataContracts;
using WCFMINERP.COM;
using System.ServiceModel;
using WCFMINERP.PrintShopBill.Presenter;
using Properties=PrintShopBill.Properties;


namespace WCFMINERP.PrintShopBill.Forms
{
    public partial class FrmLogin : Form
    {
        LoginPresenter _presenter = new PrintShopBill.Presenter.LoginPresenter();
        public FrmLogin()
        {
            InitializeComponent();
        }
        private void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                if (checkBoxRememnerUserName.Checked)
                {
                    Properties.Settings.Default.IsRememberUseName = true;
                    Properties.Settings.Default.UserName = txtUser.Text;
                }
                else
                {
                    Properties.Settings.Default.IsRememberUseName = false;
                    Properties.Settings.Default.UserName = "";
                }
                if (checkBoxRememberPwd.Checked)
                {
                    Properties.Settings.Default.IsRememberPwd = true;
                    Properties.Settings.Default.UserPwd = txtPwd.Text;
                }
                else
                {
                    Properties.Settings.Default.IsRememberPwd = false;
                    Properties.Settings.Default.UserPwd = "";
                }
                Properties.Settings.Default.Save();
                UserInfo userInfo = null;
                int ret = CheckInputData();
                if (ret == 0)
                {
                    userInfo = _presenter.Login(txtUser.Text.Trim(), Md5HashCode.GetHash(txtPwd.Text.Trim()));
                }
                if (ret == 1)
                {
                    txtUser.Select();
                    MessageBox.Show("请输入用户名称！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                if (ret == 2)
                {
                    txtPwd.Select();
                    MessageBox.Show("请输入用户密码！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                if (userInfo != null)
                {
                    InnerFactoryParameter.UserInfo = userInfo;
                    InnerFactoryParameter.TechId = _presenter.GetTechId();
                    FrmMain frm = new FrmMain();
                    frm.Show();

                    this.Hide();
                }
                else
                {
                    MessageBox.Show("输入的用户名称或密码错误！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                if (ex is EndpointNotFoundException)
                {
                    MessageBox.Show("连接服务器失败！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }

        }

        private void FrmLogin_Load(object sender, EventArgs e)
        {
            
        }


        /// <summary>
        /// 检查输入信息
        /// </summary>
        /// <returns></returns>
        private int CheckInputData()
        {
            int ret = 0;

            if (string.IsNullOrEmpty(txtUser.Text.Trim()))
            {
                ret = 1;
            }
            if (string.IsNullOrEmpty(txtPwd.Text.Trim()))
            {
                ret = 2;
            }

            return ret;
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmLogin_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void btnQuit_Click_1(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void FrmLogin_Load_1(object sender, EventArgs e)
        {

        }


    }
}
